<template>
  <div>
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        total: total,
        pageSize: page.pageSize,
        current: page.current,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag
            v-for="(tag, index) of JSON.parse(record.tags)"
            :key="index"
            color="green"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #judgeCase="{ record }">
        <a-descriptions :column="{ xs: 1, md: 3, lg: 4 }">
          <a-descriptions-item
            v-for="(item, index) of JSON.parse(record.judgeCase)"
            label="判题用例"
            :key="index"
          >
            <a-tag>{{ item.input }}</a-tag>
            <a-tag>{{ item.output }}</a-tag>
          </a-descriptions-item>
        </a-descriptions>
      </template>
      <template #judgeConfig="{ record }">
        <a-descriptions :column="{ xs: 1, md: 3, lg: 4 }">
          <a-descriptions-item label="判题配置">
            <a-tag>{{ JSON.parse(record.judgeConfig).timeLimit }}</a-tag>
            <a-tag>{{ JSON.parse(record.judgeConfig).memoryLimit }}</a-tag>
            <a-tag>{{ JSON.parse(record.judgeConfig).stackLimit }}</a-tag>
          </a-descriptions-item>
        </a-descriptions>
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button status="danger" @click="deleteQuestion(record)"
            >删除
          </a-button>
          <a-button type="primary" @click="updateQuestion(record)"
            >编辑
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { Question, QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";

const router = useRouter();
const dataList = ref();
const page = ref({
  current: 1,
  pageSize: 10,
});
const total = ref(0);
const searchQuestion = async () => {
  const res = await QuestionControllerService.listQuestionByPageUsingPost(
    page.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败," + res.message);
  }
};
onMounted(() => {
  searchQuestion();
});

//相当于监听searchQuestion中的所有变量,当其中任何一个变量改变的时候就会执行
watchEffect(() => {
  searchQuestion();
});

const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "答案",
    dataIndex: "answer",
  },
  {
    title: "内容",
    dataIndex: "content",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过次数",
    dataIndex: "acceptedNum",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
  },
  {
    title: "收藏数量",
    dataIndex: "favourNum",
  },
  {
    title: "是否删除",
    dataIndex: "isDelete",
  },
  {
    title: "判题用例",
    slotName: "judgeCase",
  },
  {
    title: "判题配置",
    slotName: "judgeConfig",
  },
  {
    title: "提交次数",
    dataIndex: "submitNum",
  },
  {
    title: "点赞次数",
    dataIndex: "thumbNum",
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

const onPageChange = (pageCurrent: number) => {
  page.value = {
    ...page.value,
    current: pageCurrent,
  };
};

//删除题目
const deleteQuestion = async (record: Question) => {
  const res = await QuestionControllerService.deleteQuestionUsingPost(record);
  if (res.code === 0) {
    await searchQuestion();
    message.success("删除成功");
  } else {
    message.error("删除失败", res.message);
  }
};

const updateQuestion = (record: Question) => {
  router.push({
    path: "/updateQuestion",
    query: {
      id: record.id,
    },
  });
};
</script>
